from django.contrib.auth.hashers import check_password
from django.db import models
from django.utils import timezone


# 系统配置表
class SysConfig(models.Model):
    config_name = models.CharField(max_length=50, verbose_name="配置名称")
    config_key = models.CharField(max_length=50, verbose_name="配置Key")
    config_value = models.CharField(max_length=100, verbose_name="配置值")
    remark = models.CharField(max_length=200, null=True, blank=True, verbose_name="描述、备注")
    create_time = models.DateTimeField(default=timezone.now, verbose_name="创建时间")
    create_by = models.BigIntegerField(verbose_name="创建人ID")
    update_time = models.DateTimeField(null=True, blank=True, verbose_name="更新时间")
    update_by = models.BigIntegerField(null=True, blank=True, verbose_name="更新人ID")
    is_deleted = models.BooleanField(default=False, verbose_name="逻辑删除标识")

    class Meta:
        db_table = 'sys_config'
        verbose_name = '系统配置'
        verbose_name_plural = verbose_name


# 部门表
class SysDept(models.Model):
    name = models.CharField(max_length=100, default='', verbose_name='部门名称')
    code = models.CharField(max_length=100, unique=True, verbose_name='部门编号')
    parent_id = models.BigIntegerField(default=0, verbose_name='父节点ID')
    tree_path = models.CharField(max_length=255, default='', verbose_name='父节点路径')
    sort = models.SmallIntegerField(default=0, verbose_name='显示顺序')
    status = models.BooleanField(default=True, verbose_name='状态(1-正常 0-禁用)')
    create_by = models.BigIntegerField(null=True, blank=True, verbose_name='创建人ID')
    create_time = models.DateTimeField(null=True, blank=True, verbose_name='创建时间')
    update_by = models.BigIntegerField(null=True, blank=True, verbose_name='修改人ID')
    update_time = models.DateTimeField(null=True, blank=True, verbose_name='更新时间')
    is_deleted = models.BooleanField(default=False, verbose_name='逻辑删除标识')

    class Meta:
        db_table = 'sys_dept'
        verbose_name = '部门'
        verbose_name_plural = verbose_name


# 字典类型表
class SysDict(models.Model):
    name = models.CharField(max_length=50, default='', verbose_name='类型名称')
    code = models.CharField(max_length=50, unique=True, verbose_name='类型编码')
    status = models.BooleanField(default=True, verbose_name='状态(0:正常;1:禁用)')
    remark = models.CharField(max_length=255, null=True, blank=True, verbose_name='备注')
    create_time = models.DateTimeField(null=True, blank=True, verbose_name='创建时间')
    update_time = models.DateTimeField(null=True, blank=True, verbose_name='更新时间')
    is_deleted = models.BooleanField(default=False, verbose_name='是否删除')

    class Meta:
        db_table = 'sys_dict'
        verbose_name = '字典类型'
        verbose_name_plural = verbose_name


# 字典数据表
class SysDictItem(models.Model):
    dict_id = models.BigIntegerField(null=True, verbose_name='字典ID')
    name = models.CharField(max_length=50, default='', verbose_name='字典项名称')
    value = models.CharField(max_length=50, default='', verbose_name='字典项值')
    status = models.BooleanField(default=True, verbose_name='状态(1-正常 0-禁用)')
    sort = models.IntegerField(default=0, verbose_name='排序')
    remark = models.CharField(max_length=255, default='', verbose_name='备注')
    create_time = models.DateTimeField(null=True, blank=True, verbose_name='创建时间')
    update_time = models.DateTimeField(null=True, blank=True, verbose_name='更新时间')

    class Meta:
        db_table = 'sys_dict_item'
        verbose_name = '字典数据'
        verbose_name_plural = verbose_name


# 系统日志表
class SysLog(models.Model):
    LOG_TYPE_CHOICES = [
        ('LOGIN', '登录'),
        ('USER', '用户'),
        ('ROLE', '角色'),
        ('DEPT', '部门'),
        ('MENU', '菜单'),
        ('DICT', '字典'),
        ('OTHER', '其他'),
    ]
    module = models.CharField(max_length=20, choices=LOG_TYPE_CHOICES, verbose_name='日志模块')
    content = models.CharField(max_length=255, verbose_name='日志内容')
    request_uri = models.CharField(max_length=255, null=True, blank=True, verbose_name='请求路径')
    ip = models.CharField(max_length=45, null=True, blank=True, verbose_name='IP地址')
    province = models.CharField(max_length=100, null=True, blank=True, verbose_name='省份')
    city = models.CharField(max_length=100, null=True, blank=True, verbose_name='城市')
    execution_time = models.BigIntegerField(null=True, blank=True, verbose_name='执行时间(ms)')
    browser = models.CharField(max_length=100, null=True, blank=True, verbose_name='浏览器')
    browser_version = models.CharField(max_length=100, null=True, blank=True, verbose_name='浏览器版本')
    os = models.CharField(max_length=100, null=True, blank=True, verbose_name='终端系统')
    create_by = models.BigIntegerField(null=True, blank=True, verbose_name='创建人ID')
    create_time = models.DateTimeField(null=True, blank=True, verbose_name='创建时间')
    is_deleted = models.BooleanField(default=False, verbose_name='逻辑删除标识')

    class Meta:
        db_table = 'sys_log'
        verbose_name = '系统日志'
        verbose_name_plural = verbose_name


# 菜单管理表
class SysMenu(models.Model):
    parent_id = models.BigIntegerField(verbose_name='父菜单ID')
    tree_path = models.CharField(max_length=255, null=True, blank=True, verbose_name='父节点ID路径')
    name = models.CharField(max_length=64, verbose_name='菜单名称')
    type = models.PositiveSmallIntegerField(verbose_name='菜单类型')
    route_name = models.CharField(max_length=255, null=True, blank=True, verbose_name='路由名称')
    route_path = models.CharField(max_length=128, null=True, blank=True, verbose_name='路由路径')
    component = models.CharField(max_length=128, null=True, blank=True, verbose_name='组件路径')
    perm = models.CharField(max_length=128, null=True, blank=True, verbose_name='权限标识')
    always_show = models.BooleanField(default=False, verbose_name='始终显示')
    keep_alive = models.BooleanField(default=False, verbose_name='页面缓存')
    visible = models.BooleanField(default=True, verbose_name='显示状态')
    sort = models.IntegerField(default=0, verbose_name='排序')
    icon = models.CharField(max_length=64, null=True, blank=True, verbose_name='菜单图标')
    redirect = models.CharField(max_length=128, null=True, blank=True, verbose_name='跳转路径')
    create_time = models.DateTimeField(null=True, blank=True, verbose_name='创建时间')
    update_time = models.DateTimeField(null=True, blank=True, verbose_name='更新时间')
    params = models.JSONField(null=True, blank=True, verbose_name='路由参数')
    # `type` tinyint NOT NULL COMMENT '菜单类型（1-菜单 2-目录 3-外链 4-按钮）',

    class Meta:
        db_table = 'sys_menu'
        verbose_name = '菜单管理'
        verbose_name_plural = verbose_name


# 用户表
class SysUser(models.Model):
    username = models.CharField(max_length=64, unique=True, verbose_name='用户名')
    nickname = models.CharField(max_length=64, verbose_name='昵称')
    gender = models.PositiveSmallIntegerField(default=1, verbose_name='性别')
    password = models.CharField(max_length=100, verbose_name='密码')
    dept = models.ForeignKey(SysDept, null=True, blank=True, on_delete=models.SET_NULL, verbose_name='部门')
    avatar = models.CharField(max_length=255, default='', verbose_name='用户头像')
    mobile = models.CharField(max_length=20, null=True, blank=True, verbose_name='联系方式')
    status = models.BooleanField(default=True, verbose_name='状态')
    email = models.EmailField(null=True, blank=True, verbose_name='用户邮箱')
    create_time = models.DateTimeField(null=True, blank=True, verbose_name='创建时间')
    create_by = models.BigIntegerField(null=True, blank=True, verbose_name='创建人ID')
    update_time = models.DateTimeField(null=True, blank=True, verbose_name='更新时间')
    update_by = models.BigIntegerField(null=True, blank=True, verbose_name='修改人ID')
    is_deleted = models.BooleanField(default=False, verbose_name='逻辑删除标识')

    class Meta:
        db_table = 'sys_user'
        verbose_name = '用户信息'
        verbose_name_plural = verbose_name
    def check_password(self, raw_password):
        return check_password(raw_password, self.password)

    class Meta:
        db_table = 'sys_user'


# 角色表
class SysRole(models.Model):
    name = models.CharField(max_length=64, unique=True, verbose_name='角色名称')
    code = models.CharField(max_length=32, unique=True, verbose_name='角色编码')
    sort = models.IntegerField(null=True, blank=True, verbose_name='显示顺序')
    status = models.BooleanField(default=True, verbose_name='角色状态')
    data_scope = models.PositiveSmallIntegerField(null=True, blank=True, verbose_name='数据权限')
    create_by = models.BigIntegerField(null=True, blank=True, verbose_name='创建人ID')
    create_time = models.DateTimeField(null=True, blank=True, verbose_name='创建时间')
    update_by = models.BigIntegerField(null=True, blank=True, verbose_name='更新人ID')
    update_time = models.DateTimeField(null=True, blank=True, verbose_name='更新时间')
    is_deleted = models.BooleanField(default=False, verbose_name='逻辑删除标识')

    class Meta:
        db_table = 'sys_role'
        verbose_name = '角色'
        verbose_name_plural = verbose_name


# 角色和菜单关联表
class SysRoleMenu(models.Model):
    role = models.ForeignKey(SysRole, on_delete=models.CASCADE, verbose_name='角色ID')
    menu = models.ForeignKey(SysMenu, on_delete=models.CASCADE, verbose_name='菜单ID')

    class Meta:
        db_table = 'sys_role_menu'
        verbose_name = '角色菜单关联'
        verbose_name_plural = verbose_name
        unique_together = ('role', 'menu')


# 用户和角色关联表
class SysUserRole(models.Model):
    user = models.ForeignKey(SysUser, on_delete=models.CASCADE, verbose_name='用户ID')
    role = models.ForeignKey(SysRole, on_delete=models.CASCADE, verbose_name='角色ID')

    class Meta:
        db_table = 'sys_user_role'
        verbose_name = '用户角色关联'
        verbose_name_plural = verbose_name
        unique_together = ('user', 'role')


# 系统消息表
class SysMessage(models.Model):
    create_by = models.BigIntegerField(null=True, blank=True, verbose_name='创建人ID')
    create_time = models.DateTimeField(null=True, blank=True, verbose_name='创建时间')
    update_by = models.BigIntegerField(null=True, blank=True, verbose_name='修改人ID')
    update_time = models.DateTimeField(null=True, blank=True, verbose_name='更新时间')
    is_deleted = models.BooleanField(default=False, verbose_name='逻辑删除标识')

    class Meta:
        db_table = 'sys_message'
        verbose_name = '系统消息'
        verbose_name_plural = verbose_name


# 公告通知表
class SysNotice(models.Model):
    title = models.CharField(max_length=50, verbose_name='通知标题')
    content = models.TextField(verbose_name='通知内容')
    notice_type = models.IntegerField(verbose_name='通知类型')
    release_by = models.BigIntegerField(null=True, blank=True, verbose_name='发布人')
    priority = models.PositiveSmallIntegerField(verbose_name='优先级')
    tar_type = models.PositiveSmallIntegerField(verbose_name='目标类型')
    tar_ids = models.TextField(verbose_name='目标人ID')
    release_status = models.PositiveSmallIntegerField(verbose_name='发布状态')
    release_time = models.DateTimeField(null=True, blank=True, verbose_name='发布时间')
    recall_time = models.DateTimeField(null=True, blank=True, verbose_name='撤回时间')
    create_by = models.BigIntegerField(verbose_name='创建人ID')
    create_time = models.DateTimeField(default=timezone.now, verbose_name='创建时间')
    update_by = models.BigIntegerField(null=True, blank=True, verbose_name='更新人ID')
    update_time = models.DateTimeField(null=True, blank=True, verbose_name='更新时间')
    is_deleted = models.BooleanField(default=False, verbose_name='逻辑删除标识')

    class Meta:
        db_table = 'sys_notice'
        verbose_name = '通知公告'
        verbose_name_plural = verbose_name


# 用户公告状态表
class SysNoticeStatus(models.Model):
    notice = models.ForeignKey(SysNotice, on_delete=models.CASCADE, verbose_name='公告ID')
    user = models.ForeignKey(SysUser, on_delete=models.CASCADE, verbose_name='用户ID')
    read_status = models.BooleanField(default=False, verbose_name='读取状态')
    read_time = models.DateTimeField(null=True, blank=True, verbose_name='阅读时间')

    class Meta:
        db_table = 'sys_notice_status'
        verbose_name = '用户公告状态'
        verbose_name_plural = verbose_name


# 代码生成基础配置表
class GenConfig(models.Model):
    table_name = models.CharField(max_length=100, unique=True, verbose_name='表名')
    module_name = models.CharField(max_length=100, null=True, blank=True, verbose_name='模块名')
    package_name = models.CharField(max_length=255, verbose_name='包名')
    business_name = models.CharField(max_length=100, verbose_name='业务名')
    entity_name = models.CharField(max_length=100, verbose_name='实体类名')
    author = models.CharField(max_length=50, verbose_name='作者')
    parent_menu_id = models.BigIntegerField(null=True, blank=True, verbose_name='上级菜单ID')
    create_time = models.DateTimeField(null=True, blank=True, verbose_name='创建时间')
    update_time = models.DateTimeField(null=True, blank=True, verbose_name='更新时间')
    is_deleted = models.BooleanField(default=False, verbose_name='是否删除')

    class Meta:
        db_table = 'gen_config'
        verbose_name = '代码生成配置'
        verbose_name_plural = verbose_name


# 代码生成字段配置表
class GenFieldConfig(models.Model):
    config = models.ForeignKey(GenConfig, on_delete=models.CASCADE, verbose_name='关联的配置ID')
    column_name = models.CharField(max_length=100, null=True, blank=True, verbose_name='列名')
    column_type = models.CharField(max_length=50, null=True, blank=True, verbose_name='列类型')
    column_length = models.IntegerField(null=True, blank=True, verbose_name='列长度')
    field_name = models.CharField(max_length=100, verbose_name='字段名称')
    field_type = models.CharField(max_length=100, null=True, blank=True, verbose_name='字段类型')
    field_sort = models.IntegerField(null=True, blank=True, verbose_name='字段排序')
    field_comment = models.CharField(max_length=255, null=True, blank=True, verbose_name='字段描述')
    max_length = models.IntegerField(null=True, blank=True, verbose_name='最大长度')
    is_required = models.BooleanField(default=False, verbose_name='是否必填')
    is_show_in_list = models.BooleanField(default=False, verbose_name='是否在列表显示')
    is_show_in_form = models.BooleanField(default=False, verbose_name='是否在表单显示')
    is_show_in_query = models.BooleanField(default=False, verbose_name='是否在查询条件显示')
    query_type = models.PositiveSmallIntegerField(null=True, blank=True, verbose_name='查询方式')
    form_type = models.PositiveSmallIntegerField(null=True, blank=True, verbose_name='表单类型')
    dict_type = models.CharField(max_length=50, null=True, blank=True, verbose_name='字典类型')
    create_time = models.DateTimeField(default=timezone.now, verbose_name='创建时间')
    update_time = models.DateTimeField(default=timezone.now, verbose_name='更新时间')

    class Meta:
        db_table = 'gen_field_config'
        verbose_name = '代码生成字段配置'
        verbose_name_plural = verbose_name
